// 百度地图关于我们地图名片
~ function ($) {
  $.fn.mapCard = function (options) {
    window.BMAP_PROTOCOL = "https";
    window.BMap_loadScriptTime = (new Date).getTime();
    var mapScript = 'https://api.map.baidu.com/getscript?v=3.0&ak=9GUMtNmq3nX5scEmqxIIh9ZYcqe7MQyv&services=&t=20180914170655';
    var defaultOpts = {
      point: false,
      zoom: 14,
      navPos: 'top_left',
      navType: 'large',
      scalePos: 'bottom_left',
      overviewPos: '',
      markers: [],
    };
    // 初始化地图
    function initMap() {
      var self = this;
      var city = null;
      // 定位城市
      if (options.options.point) {
        newMap.call(self);
      } else {
        city = new BMap.LocalCity();
        city.get(function (result) {
          // self.map.setCenter(result.name);
          // self.options.point = result.center;
          newMap.call(self);
        });
      }
    }

    // 创建地图
    function newMap() {
      var point = new BMap.Point(options.options.point.lng, options.options.point.lat);
      var map = new BMap.Map(options.id);  // 在百度地图容器中创建一个地图

      map.centerAndZoom(point, options.options.zoom);  // 设定地图的中心点和坐标并将地图显示在地图容器中
      this.map = map;

      setMapEvent.call(this);
      addMapControl.call(this);
      addMarker.call(this);
    }

    // 地图事件设置
    function setMapEvent() {
      this.map.enableDragging(); // 启用地图拖拽事件，默认启用(可不写)
      this.map.enableScrollWheelZoom(); // 启用地图滚轮放大缩小
      this.map.enableDoubleClickZoom(); // 启用鼠标双击放大，默认启用(可不写)
      this.map.enableKeyboard(); // 启用键盘上下左右键移动地图
    }

    // 地图控件添加
    function addMapControl() {
      // 向地图中添加缩放控件
      if (options.options.navPos) {
        this.map.addControl(new BMap.NavigationControl({
          anchor: window['BMAP_ANCHOR_' + options.options.navPos.toUpperCase()],
          type: window['BMAP_NAVIGATION_CONTROL_' + options.options.navType.toUpperCase()],
        }));
      }

      // 向地图中添加比例尺控件
      if (options.options.scalePos) {
        this.map.addControl(new BMap.ScaleControl({
          anchor: window['BMAP_ANCHOR_' + options.options.scalePos.toUpperCase()]
        }));
      }

      // 向地图中添加缩略图控件
      if (options.options.overviewPos) {
        this.map.addControl(new BMap.OverviewMapControl({
          anchor: window['BMAP_ANCHOR_' + options.options.overviewPos.toUpperCase()],
          isOpen: true
        }));
      }
    }

    // 添加标注
    function addMarker() {
      var markers = options.options.markers;
      var marker = null;
      var point = null;
      var info = null;
      var infoWindow = null;
      var infoLabel = null;

      for (var i = 0, l = markers.length; i < l; i++) {
        info = markers[i];
        point = new BMap.Point(info.point.lng, info.point.lat);
				// iconImg = new BMap.Icon("public/images/xiao.png", new BMap.Size(28, 38));
        marker = new BMap.Marker(point);

        this.map.addOverlay(marker);

        infoWindow = new BMap.InfoWindow(info.content || '', {
          maxWidth: 220,
          width: 220,
          title: info.title,
        });

        if (info.isAnimate) {
          marker.setAnimation(BMAP_ANIMATION_BOUNCE);
        }

        if (info.isDrag) {
          marker.enableDragging();
        }

        if (info.title) {
          infoLabel = new BMap.Label(info.title, { offset: new BMap.Size(20, -10) });
          // marker.setLabel(infoLabel);
        }

        if (info.isOpenInfo) {
          marker.openInfoWindow(infoWindow);
          infoLabel.hide();
        }

        bindMarkerEvent.call(this, marker, info, infoWindow, infoLabel);
      };
    };

    function bindMarkerEvent(marker, info, infoWindow, infoLabel) {
      var self = this;
      marker.addEventListener('dragend', function (e) {
        info.point = e.point;
      });
      marker.addEventListener("click", function () {
        marker.openInfoWindow(infoWindow); // 开启信息窗口
      });

      infoWindow.addEventListener('open', function () {
        infoLabel.hide();
        info.isOpenInfo = 1;
      });
      infoWindow.addEventListener('close', function () {
        infoLabel.show();
        info.isOpenInfo = 0;
      });
    };

    function mergeOptions(options) {
      var result = JSON.parse(JSON.stringify(defaultOpts));
      for (var i in options) {
        if (!options.hasOwnProperty(i)) continue;
        result[i] = options[i];
      }
      return result;
    };

    // 实例方法
    function search(text) {
      var local = new BMap.LocalSearch(this.map, {
        renderOptions: { map: this.map }
      });
      local.search(text);
    };
    function update(options, isJump) {
      if (options === null) { // 重置地图
        options.options = JSON.parse(JSON.stringify(defaultOpts));
      } else if (typeof options === 'object') {   // 新的配置
        options.options = options;
      }
      if (!isJump) {  // 默认刷新地图时中心点为当前显示的位置，如需要跳转到其他位置，该参数设置为true
        options.options.point = this.map.getCenter();
        options.options.zoom = this.map.getZoom();
      }
      initMap.call(this);
    };
    function getOptions() {
      if (this.map) {
        options.options.point = this.map.getCenter();
        options.options.zoom = this.map.getZoom();
      }
      return options.options;
    };
    function getPoint() {
      return this.map.getCenter();
    };
    function createMarker(title, content) {
      var markers = options.options.markers;
      markers.push({
        point: this.getPoint(),
        title: title,
        content: content,
        isDrag: 0,
        isAnimate: 0,
        isOpenInfo: 0
      });
      this.update();
    };

    function createMap(conf) {
      var t = null;
      var count = 0;
      var self = {
        id: conf.id,
        map: null,
        options: mergeOptions(conf.options),
        search: search,
        update: update,
        getOptions: getOptions,
        getPoint: getPoint,
        createMarker: createMarker
      };

      if (!document.querySelector('[data-mapScript]')) {
        var script = document.createElement('script');
        script.src = mapScript;
        script.setAttribute('data-mapScript', true);
        document.head.appendChild(script);
      };
      t = setInterval(function () {
        if (typeof BMap !== 'undefined') {
          clearInterval(t);
          initMap.call(self);
        }
        if (count >= 20) clearInterval(t);
        count++;
      }, 800);

      return self;
    };
    initMap()
    return {
      createMap: createMap
    }
  }
}(jQuery);
function pageScroll() {
    var documentScrollTop = $(document).scrollTop();
    if (documentScrollTop > 10) {
      $(".header").addClass("header-static");
    } else {
      $(".header").removeClass("header-static");
    }
  }
  $(document).scroll(pageScroll);